<?php

/**
 * AuthController
 * 
 * @author
 * @version 
 */

require_once 'BaseController.php';

class AuthController extends BaseController {
	
	function init() {
		parent::init ();
		Zend_Loader::loadClass ( 'Zend_Filter_StripTags' );
		// setup Zend_Auth adapter for a database table
		Zend_Loader::loadClass ( 'Zend_Auth_Adapter_DbTable' );
	
	}
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$this->_forward ( "login" );
	}
	
	public function loginAction() {
		$errores = array ( );
		if (isset($this->view->mensajesValidacion)) {
			$errores = array_merge($errores, $this->view->mensajesValidacion);
		}
		$this->view->contrasenaIncorrecta = false;
		if ($this->_request->isPost ()) {
			$f = new Zend_Filter_StripTags ( );
			$credencial = $f->filter ( $this->_request->getPost ( 'credencial' ) );
			$contrasena = $f->filter ( $this->_request->getPost ( 'contrasena' ) );
			if (empty ( $credencial )) {
				$errores [] = 'Debe indicar una credencial';
			} else {
				$registry = Zend_Registry::getInstance ();
				$db = $registry->get ( 'db' );
				$authAdapter = new Rnaasv_AuthAdapterDbTable($db);
				$authAdapter->setTableName ( 'qryUsuarios' );
				$authAdapter->setIdentityColumn ( 'credencial' );
				$authAdapter->setCredentialColumn ( 'contrasena' );
				$authAdapter->setTablaRoles(new TempMiembrosModel());
				$authAdapter->setColumnaIdRoles('Credencial');
				
				// Set the input credential values to authenticate against
				$authAdapter->setIdentity ( $credencial );
				$authAdapter->setCredential ( $contrasena );
				
				// do the authentication
				$auth = Zend_Auth::getInstance ();
				$result = $auth->authenticate ( $authAdapter );
				if ($result->isValid ()) {
					// success: store database row to auth's storage
					// system. (Not the password though!)
					$data = $authAdapter->getResultRowObject ( null, 'contrasena' );
					$auth->getStorage ()->write ( $data );
					$this->_redirect ( '/' );
				} else {
					// failure: clear database row from session
					$errores [] = 'Inicio de sesi�n fallido.';
					switch ( $result->getCode ()) {
						
						case Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND :
							/** do stuff for nonexistent identity **/
							$this->_redirect("/inscripcion/index?credencial=" . $credencial);
						break;
						
						case Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID :
							/** do stuff for invalid credential **/
							$this->_redirect("/inscripcion/index?credencial=" . $credencial);
						break;
						
						case Zend_Auth_Result::SUCCESS :
							/** do stuff for successful authentication **/
							$this->_redirect ( '/' );
						break;
						
						default :
							$errores = $result->getMessages();
							/** do stuff for other failure **/
						break;
					}
				}
			}
		} else {
			//die('G');
			$errores [] = 'Inicio de sesión sin formulario.';
		}
		$this->view->mensajesValidacion = $errores;
	}
	
	function logoutAction() {
		Zend_Auth::getInstance ()->clearIdentity ();
		$authNamespace = new Zend_Session_Namespace('Zend_Auth');
		$authNamespace->unsetAll();
		//var_dump($authNamespace);
		//die("E");
		$this->_redirect ( '/' );
	}
}
